package com.yonyou.pmclouds.alterandvisacategory.rmiimpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.alterandvisacategory.entity.AlterAndVisaCategoryVO;
import com.yonyou.pmclouds.alterandvisacategory.mapper.AlterAndVisaCategoryMapper;
import com.yonyou.pmclouds.alterandvisacategory.process.AlterAndVisaCategoryDeleteCheckProcessor;
import com.yonyou.pmclouds.alterandvisacategory.process.AlterAndVisaCategoryInsertCheckProcessor;
import com.yonyou.pmclouds.alterandvisacategory.process.AlterAndVisaCategoryUpdateCheckProcessor;
import com.yonyou.pmclouds.alterandvisacategory.rmiitf.AlterAndVisaCategoryMaintain;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.ConcurrentOperationException;
import com.yonyou.pmclouds.basecom.lock.AutoReleaseLockUtil;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= AlterAndVisaCategoryMaintain.class)
public class AlterAndVisaCategoryMaintainImpl implements AlterAndVisaCategoryMaintain{

    public static final String LOCK_PREFIX = "alterandvisacategory_";
    private static final String[] UPDATE_FIELDS = new String[]{CommonFieldConst.SNAME};

    @Autowired
    private AlterAndVisaCategoryMapper mapper;
    @Autowired
    private ReferenceChecker referenceChecker;

    @Override
    public AlterAndVisaCategoryVO insert(AlterAndVisaCategoryVO alterAndVisaCategoryVO) throws BusinessException {
        if(alterAndVisaCategoryVO == null) return null;
        //add lock
        if(!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + alterAndVisaCategoryVO.getPkTenant())) {
            throw new ConcurrentOperationException();
        }
        initInsertVO(alterAndVisaCategoryVO);  //补全字段信息
        SingleInsertBpTemplate<AlterAndVisaCategoryVO> bp = new SingleInsertBpTemplate<>(mapper);
        bp.addBeforeProcessor(new AlterAndVisaCategoryInsertCheckProcessor(mapper));
        //添加审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<AlterAndVisaCategoryVO>());

        bp.insert(alterAndVisaCategoryVO);
        return alterAndVisaCategoryVO;
    }

    @Override
    public AlterAndVisaCategoryVO update(AlterAndVisaCategoryVO alterAndVisaCategoryVO) throws BusinessException {
        if(alterAndVisaCategoryVO == null) return null;
        initInsertVO(alterAndVisaCategoryVO);  //补全字段信息
        SingleUpdateBpTemplate<AlterAndVisaCategoryVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        bp.addBeforeProcessor(new AlterAndVisaCategoryUpdateCheckProcessor(mapper));
        //审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<AlterAndVisaCategoryVO>());
        bp.update(alterAndVisaCategoryVO);
        return alterAndVisaCategoryVO;
    }

    @Override
    public String delete(String pkAlterVisaCategory, String changeFlag) throws BusinessException {
        //add lock
        if(!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + RuntimeEnvironment.getTenantId())) {
            throw new ConcurrentOperationException();
        }
        AlterAndVisaCategoryVO vo = new AlterAndVisaCategoryVO();
        vo.setPkAltervisacategory(pkAlterVisaCategory);
        vo.setChangeFlag(changeFlag);

        SingleDeleteBpTemplate<AlterAndVisaCategoryVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addBeforeProcessor(new AlterAndVisaCategoryDeleteCheckProcessor(referenceChecker));

        bp.delete(vo);
        return vo.getPkAltervisacategory();
    }


    private void initInsertVO(AlterAndVisaCategoryVO vo){
        if(vo == null) return;
        if(StringUtils.isEmpty(vo.getPkTenant())){
            vo.setPkTenant(RuntimeEnvironment.getTenantId());
        }
    }
}
